package com.app.TCES.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.app.TCES.domain.Class;
import com.app.TCES.domain.ClassSet;
import com.app.TCES.domain.Department;
import com.app.TCES.service.ClassService;

@Controller
public class ClassController {

	@Autowired
	ClassService classService;

	@RequestMapping("/ClassSet") // 管理员班级管理
	@ResponseBody
	public Map<String, Object> CourseSet(Model model) {

		List<ClassSet> classes = classService.getClassSet();

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tol", 1);
		map.put("status", 200);
		map.put("massage", "成功");
		map.put("classes", classes);

		return map;
	}

	// 删除班级
	@RequestMapping("/deleteClass")
	@ResponseBody
	public Map<String, String> deleteTeacher(Integer classId) {

		classService.deleteClass(classId);

		Map<String, String> map = new HashMap<String, String>();
		map.put("msg", "成功");
		return map;

	}

	// 修改
	@RequestMapping("/updateClass")
	@ResponseBody
	public Map<String, String> updateClass(com.app.TCES.domain.Class classes) {

		classService.updateClass(classes);

		Map<String, String> map = new HashMap<String, String>();
		map.put("msg", "YES");
		return map;

	}

	@RequestMapping("/insertClass") // 添加班级
	@ResponseBody
	public Map<String, String> insertClass(com.app.TCES.domain.Class classes) {

		Map<String, String> map = new HashMap<String, String>();

		int x = classService.selectClass(classes);
		int y = classService.selectClassName(classes);
		if (x > 0) {
			map.put("msg", "班级号被占有");
			return map;
		} else {

			if (y > 0) {
				map.put("msg", "班级名已存在");
				return map;

			} else {
				classService.insertClass(classes);

				map.put("msg", "成功");
				return map;
			}

		}

	}

	// 获取院系列表
	@ResponseBody
	@RequestMapping("/getDepartment")
	public List<Department> getDepartments() {
		return classService.getDepartments();
	}

	// 通过院系Name获取班级列表
	@ResponseBody
	@RequestMapping("/getClassList")
	public Map<String, Object> getClassList(String deptName) {
		List<Class> list = new ArrayList<Class>();
		list = classService.getClassList(deptName);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tol", 1);
		map.put("status", 200);
		map.put("massage", "成功");
		map.put("classes", list);
		return map;
	}

}
